import Vue from 'vue'
import {Toast, Indicator, MessageBox} from 'mint-ui'
import * as http from './http'
import * as natives from './platform'

window.$Utils = new Utils()
Vue.prototype.http = http
Vue.prototype.$natives = natives

function Utils() {
  let object = new Object()
  object.toast = (options) => {
    Object.assign(options, {
      duration: 4000,
      position: 'top'
    });
    return Toast(options)
  }
  object.loading = (option = {}) => {
    Indicator.open(option)
  }
  object.hideloading = () => {
    Indicator.close()
  }
  object.MessageBox = MessageBox

  object.isNull = (...objs) => {
    let bol = false, fn = function (obj) {
      if (typeof obj == 'undefined') {
        return true
      } else if (obj == null) {
        return true
      } else if (typeof obj == 'string' && (obj.trim() === '' || obj === 'undefined' || obj === 'null')) {
        return true
      } else if (typeof obj == 'object' && Array.isArray(obj) && obj.length == 0) {
        return true
      } else if (typeof obj == 'object' && !Array.isArray(obj) && Object.keys(obj).length == 0) {
        return true
      } else {
        return false
      }
    }
    bol = objs.some(obj => {
      return fn(obj)
    })
    return bol
  }

  object.getUserInfo = () => {
    let userInfo = localStorage.getItem('userInfo')
    return JSON.parse(userInfo)
  }

  /**
   * 获取年月日
   * */
  object.getDate = (addDay = 0, format = 'YYYY-MM-DD') => {
    let d = new Date()
    if (addDay instanceof Date) {
      d = addDay
    } else if (!object.isNull(addDay)) {
      d.setDate(d.getDate() + addDay * 1)//当前日期+几天
    }
    return object.dateToString(d, format)
  }

  /**
   * 日期 Date 转 String
   * */
  object.dateToString = (date = new Date(), format = 'YYYY-MM-DD') => {
    let FullYear = date.getFullYear()
    let Month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let Day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let Hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let Minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let Seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()

    return format.replace('yyyy', String(FullYear))
      .replace('mm', String(Month))
      .replace('dd', String(Day))
      .replace('hh', String(Hours))
      .replace('mi', String(Minutes))
      .replace('ss', String(Seconds))
      .replace('YYYY', String(FullYear))
      .replace('MM', String(Month))
      .replace('DD', String(Day))
      .replace('HH', String(Hours))
      .replace('MI', String(Minutes))
      .replace('SS', String(Seconds))
  }

  object.getYear = (add, format = 'YYYY') => {
    let d = new Date()
    if (add instanceof Date) {
      d = add
    } else if (!object.isNull(add)) {
      d.setFullYear(d.getFullYear() + add * 1)
    }
    return object.dateToString(d, format)
  }

  object.toLowerCase = (o) => {
    let objectToKeys = (object) => {
      let keys = Object.keys(object)
      let clone_o = {}
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i]
        clone_o[key.toLowerCase()] = object[key]
      }
      return clone_o
    }
    if (o instanceof Array) {
      let new_arr = []
      for (let i = 0; i < o.length; i++) {
        new_arr.push(objectToKeys(o[i]))
      }
      return new_arr
    } else if (o instanceof Object) {
      return objectToKeys(o)
    } else {
      return {}
    }
  }

  object.getUrlByPlatform = () => {
    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
    let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    let url = ''
    if (isIOS) {
      url = location.href.split('#')[0]  //hash后面的部分如果带上ios中config会不对
    } else {
      url = location.href
    }
    return url
  };
  return object
}

// 金额通道
Vue.filter('money', (s, type) => {
  let a = 0;
  if (/[\~!-]/g.test(s)) {
    a = s.toString().replace('-', '');
  } else {
    a = s;
  }
  if (/[^0-9\.]/.test(a))
    return '0.00'
  if (a == null || a == 'null' || a == '')
    return '0.00'
  a = a.toString().replace(/^(\d*)$/, '$1.')
  a = (a + '00').replace(/(\d*\.\d\d)\d*/, '$1')
  a = a.replace('.', ',')
  let re = /(\d)(\d{3},)/
  while (re.test(a))
    a = a.replace(re, '$1,$2')
  a = a.replace(/,(\d\d)$/, '.$1')
  if (type == 0) {
    let b = a.split('.')
    if (b[1] == '00') {
      a = b[0]
    }
  }
  if (/[\~!-]/g.test(s)) {
    return '¥' + '-' + a
  }else{
    return '¥' + a
  }
})
